/**
 * 
 */
package com.fita.entity.wallet;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fita.entity.agent.Agent;
import com.fita.entity.order.OrderPayType;

/**
 * 提现记录
 * 
 * @author Guangli Yan
 *
 *
 */
@Entity
@Table(name = "withdraw_record")
public class WithdrawRecord {

    @Id
    @GeneratedValue
    private Long id;

    /**
     *  提现金额（分）
     *
     */
    private Integer amount;


    /**
     * 当前余额（分）
     */
    private Integer currentUserWalletAmount;


    private Date createTime;

    /**
     * 支付状态
     */
    @Enumerated(EnumType.STRING)
    private WithdrawRecordPaidStatus paidStatus;

    /**
     * 支付流水号
     */
    private String payCode;

    /**
     * 支付时间
     */
    private Date payTime;

    /**
     * 支付方式
     */
    @Enumerated(EnumType.STRING)
    private OrderPayType payType;

    @ManyToOne
    @JoinColumn(nullable = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private UserWallet wallet;

    /**
     * 操作人
     */
    @ManyToOne(optional = false)
    private Agent operator;

    @Column(name = "is_disabled")
    private Boolean disabled;

    public WithdrawRecord() {

    }

    /**
     * 创建提现记录
     * @param wallet
     * @param operator
     */
    public WithdrawRecord(UserWallet wallet, Agent operator, Integer amount, Integer userWalletAmount) {

        this.amount = amount;
        this.operator = operator;
        this.wallet = wallet;
        this.paidStatus = WithdrawRecordPaidStatus.UNPAID;
        this.disabled = false;
        this.createTime = new Date();
        this.currentUserWalletAmount = userWalletAmount;
    }

    /**
     * 提现数量（元）amount除以100
     * @return
     */
    @Transient
    public BigDecimal getAmountDivide100(){
        return amount != null ? new BigDecimal(this.amount).divide(BigDecimal.valueOf(100)).setScale(2) : null;
    }

    @Transient
    public BigDecimal getCnyAmount(){
        return getAmountDivide100();
    }

    @Transient
    public BigDecimal getCnyCurrentUserWalletAmount(){
        if (currentUserWalletAmount !=null && currentUserWalletAmount > 0){
            return new BigDecimal(currentUserWalletAmount).divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP);
        }
        return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
    };

    /**
     * 同意提现
     * @param payType
     * @param payCode
     * @return
     */
    public WithdrawRecord confirm(OrderPayType payType, String payCode) {

        this.payCode = payCode;
        this.payType = payType;
        this.paidStatus = WithdrawRecordPaidStatus.PAID;
        this.payTime = new Date();

        return this;
    }

    /**
     * 获得状态
     * @return
     */
    @Transient
    public String getStatusDesc(){return this.paidStatus.getDescription();}

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public UserWallet getWallet() {
        return wallet;
    }

    public void setWallet(UserWallet wallet) {
        this.wallet = wallet;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public String getPayCode() {
        return payCode;
    }

    public void setPayCode(String payCode) {
        this.payCode = payCode;
    }

    public OrderPayType getPayType() {
        return payType;
    }

    public void setPayType(OrderPayType payType) {
        this.payType = payType;
    }

    public Date getPayTime() {
        return payTime;
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    public WithdrawRecordPaidStatus getPaidStatus() {
        return paidStatus;
    }

    public void setPaidStatus(WithdrawRecordPaidStatus paidStatus) {
        this.paidStatus = paidStatus;
    }

    public Agent getOperator() {
        return operator;
    }

    public void setOperator(Agent operator) {
        this.operator = operator;
    }

    public Integer getCurrentUserWalletAmount() {
        return currentUserWalletAmount;
    }

    public void setCurrentUserWalletAmount(Integer currentUserWalletAmount) {
        this.currentUserWalletAmount = currentUserWalletAmount;
    }
}

